package com.zhuchaooasystem.adapter.holder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.ui.home.fragment.oa.record.LocalRecordActivity;
import com.zhuchaooasystem.ui.home.fragment.oa.record.RecordActivity;
import com.zhuchaooasystem.utils.ToastUtil;

/**
 * @author yinsi
 */
public class LocalRecordHolder extends BaseViewHolder<String> {
    private TextView txt_path;
    private ImageView iv_check;
    private ImageView iv_control;
    //----
    private LocalRecordActivity activity;

    public LocalRecordHolder(ViewGroup parent, LocalRecordActivity activity) {
        super(parent, R.layout.item_activity_local_record);
        txt_path = $(R.id.item_act_local_record_txt_path);
        iv_check = $(R.id.item_act_local_record_iv_check);
        iv_control = $(R.id.item_act_local_record_iv_control);
        //----
        this.activity = activity;
    }

    @Override
    public void setData(final String record) {
        //解决上下滑动checkbox错乱问题
        iv_check.setTag(getDataPosition());
        if (null != activity.getCheckPositionlist()) {
            if (activity.getCheckPositionlist().contains(getDataPosition())) {
                iv_check.setImageResource(R.drawable.ic_oval_checked);
            } else {
                iv_check.setImageResource(R.drawable.ic_oval_uncheck);
            }
        }
        //该音乐正在播放
        if (activity.getCur_position() == getDataPosition()) {
            iv_control.setImageResource(R.drawable.ic_record_stop);
        } else {
            iv_control.setImageResource(R.drawable.ic_record_play);
        }
        //----
        txt_path.setText(record.substring(record.lastIndexOf("/") + 1));
        iv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置路径选中不选中的添加或移除
                if (activity.getUploadAudioList().contains(record)) {
                    activity.getUploadAudioList().remove(activity.getUploadAudioList().indexOf(record));
                    activity.getCheckPositionlist().remove(activity.getCheckPositionlist().indexOf(iv_check.getTag()));
                    iv_check.setImageResource(R.drawable.ic_oval_uncheck);
                } else {
                    if (activity.getUploadAudioList().size() < RecordActivity.UPLOAD_RECORD_MAX) {
                        activity.getUploadAudioList().add(record);
                        activity.getCheckPositionlist().add((Integer) iv_check.getTag());
                        iv_check.setImageResource(R.drawable.ic_oval_checked);
                    } else {
                        ToastUtil.showShort(activity, "最多上传" + RecordActivity.UPLOAD_RECORD_MAX + "个录音文件");
                    }
                }
                //设置描述文本的内容
                activity.refreshView();
            }
        });
        iv_control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onClickPlay(getDataPosition());
            }
        });
    }

}
